/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package handlers;

import checker.checkerUser;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
import readers.UserReader;

/**
 *
 * @author Chris
 */
public class loginHandler extends HttpServlet {
    static void failLogin(HttpServletRequest request, HttpServletResponse response)
    throws IOException {
        response.sendRedirect("index.jsp");
    }
    
    protected void process(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String user = request.getParameter("user");
        String pw = request.getParameter("password");
        if(user == null || pw == null) {
            failLogin(request, response);
            return;
        }
        
        checkerUser cU = new checkerUser();
        if(!cU.isValid(user, pw) ||
                !UserReader.isTeacher(user)) {
            failLogin(request, response);
            return;
        }
        Cookie cookiesUN = new Cookie("username", user);
        Cookie cookiesPW = new Cookie("password", pw);
        
        cookiesUN.setMaxAge(60*60*3);//Lasts 3 hours
        cookiesPW.setMaxAge(60*60*3);//Lasts 3 hours
        response.addCookie(cookiesUN);
        response.addCookie(cookiesPW);
        
        response.sendRedirect("index2.jsp");
        return;
    }
                
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        process(request, response);
    } 

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        process(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Handles login information";
    }
    
    public static void bootInvalidUsers(HttpServletRequest request, HttpServletResponse response) {
        try {
            int i;
            Cookie[] cookies = request.getCookies();
            Cookie cookieUN = null;
            Cookie cookiePW = null;
            if(cookies.length == 0) {
                failLogin(request, response);
            }
            
            for(i=0; i<cookies.length; i++) {
                if(cookies[i].getName().equals("username"))
                    cookieUN = cookies[i];
                
                if(cookies[i].getName().equals("password"))
                    cookiePW = cookies[i];
            }
            
            if(cookieUN == null || cookiePW == null) {
                failLogin(request, response);
            }
           
            checkerUser cU = new checkerUser();
            if(!cU.isValid(cookieUN.getValue(), cookiePW.getValue())) {
                failLogin(request, response);
            }
        }
        catch(Exception ex) {
        }
    }
}
